package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏业务:geofenceQuery={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //1.依次遍历出每个围栏对象
        for (int i = 0; i < list.size(); i++) {
            GeofenceVO geofenceVO = list.get(i);
            //2.封装车辆查询条件
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            //3.调用车辆模块的方法查询指定车辆数据
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //4.设置车辆总数
            geofenceVO.setTotalNum(vehicleVOList.size());
            //5.设置可用车辆
            int availableNum = 0;
            for(VehicleVO vehicleVO:vehicleVOList){
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            geofenceVO.setAvailableNum(availableNum);
            //6.因为后续需要查看车辆详情对车辆进行分配,所以此处也需要将车辆集合传递回去
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("保存围栏业务:geofenceParam={}", geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        if (geofence.getId() != null) {//更新
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }else {//新增
            geofence.setStatus("1");//启用
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }

    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏业务:id={}", id);
        //1.封装车辆的查询条件
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.直接调用车辆模块的数据层查询此围栏下的所有绑定车辆
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果该围栏下有车,业务层需要处理,没车就正常删除
        if(list.size()>0 && list != null){
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else{
            geofenceMapper.deleteGeofence(id);
        }
    }
}
